package com.chzsyh.evwer.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.chzsyh.evwer.R;
import com.chzsyh.evwer.WSManager;

import org.jetbrains.annotations.NotNull;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import cn.hutool.core.thread.ThreadFactoryBuilder;
import okhttp3.Response;
import okhttp3.WebSocket;
import okhttp3.WebSocketListener;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;

/**
 * 创建日期 2022/6/16
 * 描述：
 */
public class WebSocketActivity extends AppCompatActivity {

    public static void start(Context context) {
        Intent starter = new Intent(context, WebSocketActivity.class);
        context.startActivity(starter);
    }

    WebSocket mWebSocket;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web_socket);

        ThreadFactory namedThreadFactory = new ThreadFactoryBuilder().
                setNamePrefix("demo-pool-%d").build();
        ExecutorService singleThreadPool = new ThreadPoolExecutor(1, 1,
                0L, TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<Runnable>(1024), namedThreadFactory, new ThreadPoolExecutor.AbortPolicy());

        singleThreadPool.execute(()-> {
            System.out.println(Thread.currentThread().getName());
            MockWebServer mMockWebServer = new MockWebServer();
            MockResponse response = new MockResponse()
                    .withWebSocketUpgrade(new WebSocketListener() {

                        @Override
                        public void onOpen(@NotNull WebSocket webSocket, @NotNull Response response) {
                            super.onOpen(webSocket, response);
//有客户端连接时回调

                            Log.e(TAG, "服务器收到客户端连接成功回调：");
                            mWebSocket = webSocket;
                            mWebSocket.send("我是服务器，你好呀");

                        }

                        @Override

                        public void onMessage(@NotNull WebSocket webSocket, @NotNull String text) {
                            super.onMessage(webSocket, text);
                            Log.e(TAG, "服务器收到消息：" + text);

                        }

                        @Override

                        public void onClosed(@NotNull WebSocket webSocket, int code, @NotNull String reason) {
                            super.onClosed(webSocket, code, reason);
                            Log.e(TAG, "onClosed：");

                        }

                    });

            mMockWebServer.enqueue(response);
            String websocketUrl = "ws://" + mMockWebServer.getHostName() + ":" + mMockWebServer.getPort() + "/";
            websocketUrl = "ws://47.111.131.205:61614";
            Log.d(TAG, "run: " + websocketUrl);
            WSManager.getInstance().init(websocketUrl);
        });
        singleThreadPool.shutdown();


    }



    private static String TAG = "WebSocketActivity";
}
